<template>
  <el-dialog
    title="上传安装包"
    :visible.sync="uploadOpen"
    width="600px"
    append-to-body
    @open="open"
  >
    <el-form ref="uploadForm" :model="uploadForm" label-width="120px">
      <el-form-item label="菜单路径">
        <el-input v-model="uploadForm.path" disabled></el-input>
      </el-form-item>
      <el-form-item label="安装类型">
        <el-radio-group v-model="uploadForm.type" @change="typeChange">
          <el-radio label="1">全量包</el-radio>
          <el-radio label="2">增量包</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="保留上次配置" v-show="uploadForm.showKeepConf">
        <el-switch v-model="uploadForm.keepConf"></el-switch>
      </el-form-item>
      <el-form-item label="安装路径" v-show="uploadForm.showDeployName">
        <el-select
          v-model="uploadForm.deployName"
          placeholder="请选择安装包"
          style="width: 100%"
        >
          <el-option
            v-for="deployName in deployNames"
            :key="deployName"
            :label="deployName"
            :value="deployName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="安装包">
        <el-upload
          ref="upload"
          :action="uploadUrl"
          :file-list="uploadFileList"
          :auto-upload="false"
          :limit="1"
          :data="uploadForm"
          :on-success="uploadSuccess"
          :on-error="uploadError"
        >
          <el-button slot="trigger" size="small" type="primary"
            >选取文件</el-button
          >
          <div slot="tip" class="el-upload__tip">
            {{ uploadForm.tip }}
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item>
        <el-button @click="submitUpload">确定</el-button>
        <el-button @click="cancelUpload">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { getUploadUrl } from "@/api/console";
import store from "@/store";
export default {
  data() {
    return {
      // 上传文件列表
      uploadFileList: [],
      // 上传地址
      uploadUrl: getUploadUrl(),
      uploadForm: {
        path: this.path,
        type: "1",
        showKeepConf: true,
        showDeployName: false,
        tip: "全量包只能上传zip文件",
        keepConf: true,
        deployName: "",
      },
    };
  },
  props: {
    pathP: "",
    deployNamesP: "",
    uploadOpenP: false,
  },
  created() {},
  activated() {},
  computed: {
    uploadOpen: {
      set: function () {
        this.$emit("on-close", "");
        this.$refs.upload.uploadFiles.splice(
          0,
          this.$refs.upload.uploadFiles.length
        );
      },
      get: function () {
        return this.uploadOpenP;
      },
    },
    deployNames: {
      get: function () {
        return this.deployNamesP;
      },
    },
    path: {
      get: function () {
        return this.pathP;
      },
    },
  },
  methods: {
    open() {
      this.uploadForm = {
        path: this.path,
        type: "1",
        showKeepConf: true,
        showDeployName: false,
        tip: "全量包只能上传zip文件",
        keepConf: true,
        deployName: "",
      };
    },
    typeChange(value) {
      if (value === "1") {
        this.uploadForm.showKeepConf = true;
        this.uploadForm.showDeployName = false;
        this.uploadForm.tip = "全量包只能上传zip文件";
      } else if (value === "2") {
        this.uploadForm.showKeepConf = false;
        this.uploadForm.showDeployName = true;
        this.uploadForm.tip = "增量包只能上传jar文件";
      }
    },
    // 提交上传
    submitUpload() {
      if (this.$refs.upload.uploadFiles.length == 0) {
        this.msgError("请选择文件");
        return;
      }
      const filename = this.$refs.upload.uploadFiles[0].name;
      if (filename.indexOf("(") > -1 || filename.indexOf(")") > -1) {
        this.msgError("文件格式错误");
        return;
      }
      if (this.uploadForm.type === "1" && filename.lastIndexOf(".zip") == -1) {
        this.msgError("文件格式错误");
        return;
      }
      if (this.uploadForm.type === "2" && filename.lastIndexOf(".jar") == -1) {
        this.msgError("文件格式错误");
        return;
      }
      if (this.uploadForm.type === "2" && this.uploadForm.deployName === "") {
        this.msgError("请选择安装路径");
        return;
      }
      console.log(this.uploadForm.deployName);
      if (
        this.uploadForm.deployName &&
        "jwater-lib" === this.uploadForm.deployName &&
        filename.indexOf("jwater-core") == -1
      ) {
        this.msgError("系统路径下不能传其他jar包");
        return;
      }
      if (
        this.uploadForm.deployName &&
        "jwater-console" === this.uploadForm.deployName &&
        filename.indexOf("jwater-console") == -1
      ) {
        this.msgError("系统路径下不能传其他jar包");
        return;
      }
      this.uploadForm.regionName = store.getters.regionName;
      this.$refs.upload.submit();
    },
    cancelUpload() {
      this.uploadForm = {};
      this.uploadOpen = false;
      this.$refs.upload.uploadFiles.splice(
        0,
        this.$refs.upload.uploadFiles.length
      );
    },
    uploadSuccess(res, file, fileList) {
      if (res.success) {
        this.uploadOpen = false;
        this.$refs.upload.uploadFiles.splice(
          0,
          this.$refs.upload.uploadFiles.length
        );
        this.$emit("on-upload-success", file, fileList);
      } else {
        this.msgError("上传失败:" + res.msg);
      }
    },
    uploadError(res, file, fileList) {
      this.msgError("接口错误");
    },
  },
};
</script>